package com.douban.view
{
	import com.douban.event.SeekEvent;
	import com.douban.utils.Motion;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.net.SharedObject;
	import flash.utils.Timer;
	import flash.utils.clearTimeout;
	import flash.utils.setTimeout;
	
	import mx.controls.Image;
	import mx.core.UIComponent;
	import mx.effects.easing.Linear;
	
	public class VolControler extends UIComponent
	{
		[Embed(source='/res/volumn.png')]
		private var labaCls:Class;
		
		private var isDragging:Boolean;
		private var showControl:Boolean;
		private var mySo:SharedObject;
		private var timer:Timer;
		private var laba:Image;
		private var bar:VolControlBar;
		private var hideTimeout:uint;
		private var _vol:Number;
		private var clickBar:Sprite;
		public function VolControler(width:Number)
		{
			super();
			this.width = width;
			
			var cls:Class = labaCls;
			var bmp:Bitmap = new cls() as Bitmap;
			laba = new Image();
			laba.source = bmp;
			laba.width = 13;
			laba.height = 10;
			laba.y = 0;
			laba.x = this.width - bmp.width;
			this.addChild(laba);
			
			bar = new VolControlBar(width-bmp.width);
			bar.x = bmp.width;
			bar.y = 3;
			this.addChild(bar);
			
			isDragging = false;
			showControl = true;
			laba.addEventListener(MouseEvent.MOUSE_OVER, onOver);
			addEventListener(MouseEvent.MOUSE_OUT, onOut);
			this.mySo = SharedObject.getLocal("mp3player", "/");
			if (this.mySo.data.volume != undefined)
			{
				this.setVolume(parseFloat(mySo.data.volume));
			}
			else
			{
				this.setVolume(1);
			}
			bar.buttonMode = true;
			laba.buttonMode = true;
			
			this.clickBar = new Sprite();
			this.clickBar.x = -1000;
			this.clickBar.graphics.beginFill(0);
			this.clickBar.graphics.drawRect(0, 0, this.width-laba.width + 10, 23);
			this.clickBar.graphics.endFill();
			this.clickBar.buttonMode = true;
			this.clickBar.alpha = 0;
			clickBar.addEventListener(MouseEvent.MOUSE_OVER, onOver);
			clickBar.addEventListener(MouseEvent.MOUSE_DOWN, this.onStartDrag);
			clickBar.addEventListener(MouseEvent.MOUSE_UP, this.onStopDrag);
			addChild(this.clickBar);
			
			timer = new Timer(40);
			timer.addEventListener(TimerEvent.TIMER, onTimer);
		}
		
		public function setVolume(vol:Number):void
		{
			if (vol > 1)
			{
				vol = 1;
			}
			if (vol < 0)
			{
				vol = 0;
			}
			var seekEvent:SeekEvent = new SeekEvent(vol);
			dispatchEvent(seekEvent);
			this._vol = vol;
			this.bar.setPos(vol);
			this.mySo.data.volume = vol;
			this.mySo.flush();
		}
		
		public function get volumn():Number{
			return this._vol;
		}
		
		public function hideMain():void{
			if (!this.showControl)
			{
				return;
			}
			if (this.isDragging)
			{
				this.delayToHide();
			}
			this.clickBar.x = -1000;
			new Motion(this.bar, "alpha", Linear.easeOut, 1, 0, 10).play();
			new Motion(this.laba, "x", Linear.easeOut, this.laba.x, this.width-laba.width, 10).play();
			showControl = false;
		}
		
		private function onOver(e:MouseEvent):void
		{
			if (this.hideTimeout)
			{
				clearTimeout(this.hideTimeout);
			}
			if (this.laba.x == this.width - laba.width)
			{
				this.showMain();
			}
		}
		
		private function showMain():void
		{
//			if (this.busy)
//			{
//				return;
//			}
//			this.busy = true;
			new Motion(this.laba, "x", Linear.easeOut, this.laba.x, 0, 10).play();
			this.clickBar.x = this.bar.x - 6;
			this.clickBar.y = this.bar.y - 10;
			this.showControl=true;
			new Motion(this.bar, "alpha", Linear.easeOut, 0, 1, 10).play();
		}
		
		private function onOut(e:MouseEvent):void
		{
			hideTimeout = setTimeout(hideMain, 100);
		}
		
		private function controllerDown(e:MouseEvent):void
		{
//			this.setVolume((this.m.controller.x - this.m.volStick.x + this.m.controller.mouseX) / this.maxWidth);
		}
		
		private function controllerUp(e:MouseEvent):void{
			this.onStopDrag(e);
		}
		
		private function onStartDrag(e:MouseEvent):void{
			delayToHide();
			if (this.isDragging)
			{
				return;
			}
			this.isDragging = true;
			this.timer.start();
		}
		
		private function onStopDrag(e:MouseEvent):void{
			this.timer.stop();
			this.isDragging = false;
		}
		
		private function onTimer(e:TimerEvent):void
		{
			if (this.clickBar.mouseX <= 0 || this.clickBar.mouseX >= this.clickBar.width || this.clickBar.mouseY <= 0 || this.clickBar.mouseY >= this.clickBar.height)
			{
				this.stopDrag();
				this.timer.stop();
			}
			else
			{
				this.setVolume((this.clickBar.mouseX - 2) / this.clickBar.width);
			}
		}
		
		private function delayToHide():void
		{
			if (this.hideTimeout)
			{
				clearTimeout(this.hideTimeout);
			}
			this.hideTimeout = setTimeout(this.hideMain, 5000);
		}
	}
}